package com.fruits.validator;

import com.fruits.util.ParameterDetectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class PhoneConstraionValidator implements ConstraintValidator<PhoneConstraion,Object> {

    private static final Logger logger = LoggerFactory.getLogger(PhoneConstraionValidator.class);

    /**
     * 初始化
     * @param constraintAnnotation
     */
    @Override
    public void initialize(PhoneConstraion constraintAnnotation) {
        logger.info("手机号码自定义验证初始化" + constraintAnnotation);
    }

    /**
     * 是否有效
     * @param value
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null || value == ""){
            logger.info("号码为空");
            return true;
        }
        if (ParameterDetectionUtil.checkPhone(value + "").getState() == 1){
            logger.info("号码不为空,且格式正确");
            return true;
        }
//        logger.info("号码不为空,但格式错误");
        return false;
    }
}
